$(document).ready(function () {

    $.ajaxSetup({cache: false});

    $('.icon-painel-online-user').popover({
        html: true,
        trigger: 'click'
    });

    $.fn.lancesOperationsInitialize();
    $('.btn-ico.images').on('click', function () {
        $('.tmb').toggleClass('hide');
    });
    var imgH = 0;
    $('.tmb img').each(function (k, v) {
        var $h = $(this).height();
        if (imgH == 0) {
            imgH = $h;
        }
        if ($h < imgH) {
            imgH = $h;
        }
    });
    $('.tmb img').css({'height': imgH, 'width': 'auto'});

    $('.maxlive-ul-li').each(function (k, v) {
        var $h = $(this).outerHeight();
        if (imgH == 0) {
            imgH = $h;
        }
        if ($h < imgH) {
            imgH = $h;
        }

    });
   console.log(window);
    var box_painel = 0;
    $('[data-trigger="show-box"]').on('click', function () {

        var obj = $('.box-painel-2'),
                w = obj.width(),
                p = parseInt(obj.css('right'), 10);


        if (p != 0) {
            obj.animate({'right': 0});

        } else {
            obj.animate({'right': '-' + w});

        }
    });


    $('[settings-fav]').off('click.ManagerFavorites').on('click.ManagerFavorites', function (e) {
        e.preventDefault();

        $.fancybox({
            type: 'ajax',
            width: 450,
            height: 350,
            scrolling: 'no',
            autoResize: false,
            autoSize: false,
            href: url_site + 'manager-favorites.html',
            afterLoad: function () {
                setInterval(function () {
                    $('[delete-favorite]').off('click').on('click', function (e) {
                        e.preventDefault();
                        var $id = $(this).attr('data-id');

                        $.ajax({'url': 'delete-favorite-' + $id + '.html',
                            dataType: 'json',
                            success: function (data) {
                                if (data.error == false) {
                                    $('tr[row="' + $id + '"]').remove();

                                }
                                $('.message-favorites').html(data.message);
                            }});

                    });

                }, 100);
            }
        });
    });
    //btn-ativar

    $(".gallery-maxlive").click(function () {
        var $data = $(this).attr('data-images');
        $.fancybox.open($.parseJSON($data));
    });
    $('[show-hide-fav]').on('click.FavoritesShowHide', function (e) {
        $('.maxlive-ul-li').not('[lote-favorite]').toggleClass('hide');
    });


    var slideW = $('.slide-painel').width();

    $('.slide-painel-item').css('width', slideW + 10);
    var slidesClick = $('.maxlive-ul li.maxlive-ul-li').not('.maxlive-status-icons a'),
            slides = $('.slide-painel-item'),
            slideWidth = slides.width(),
            numberOfSlides = slides.length,
            currentPosition = 0;
    slides.wrapAll('<div id="slidesHolder"></div>');
    slides.css({'float': 'left'});



    $('#slidesHolder').css('width', slideWidth * numberOfSlides);
    function changePosition() {
        if (currentPosition == numberOfSlides - 1) {
            currentPosition = 0;
            // manageNav(currentPosition);
        } else {
            currentPosition++;
            // manageNav(currentPosition);
        }
        moveSlide();
    }


    function moveSlide() {
        $('#slidesHolder').animate({'marginLeft': slideWidth * (-currentPosition)});
    }

    $('.maxlive-ul li.maxlive-ul-li').not('.maxlive-status-icons a').on('click', function (e) {

        var $lid = $(this).attr('data-lote-id'),
                $objSelected = $('.panel-lote-' + $lid),
                $_class = $objSelected.attr('class'),
                hasActiveP = /active/gi;

        currentPosition = $(this).index();
        moveSlide();


        if (!hasActiveP.test($_class)) {

            $('.item-painel-details').fadeOut(900, function () {
                $(this).removeClass('active');

            });
            // $objSelected.fadeIn(500,function(){$(this).addClass('active');});


        }





    });
    $('.detail-maxlive').fancybox();

    $('a[data-trigger-get-info-lances]').click(function (e) {
        e.preventDefault();
        var $id = $(this).attr('lote-id');
        $.ajax({
            url: url_site + 'home/getHistoryLote/' + $id,
            success: function (html) {
                $.fancybox(html, {'width': 400});
            }
        });

    });

    $('.clock-lote-info-data,.time-lance').onTickerTime(
            {
                triggerdate: 'data-count',
                tickerInterval: 1,
                DisplayFormat: "%%D%%d %%H%%:%%M%%:%%S%%",
                update: function (element, secs) {
                    var id = $(element).attr('data-lote-id'),
                            obj = $('[data-lote-id="' + id + '"]'),
                            objAnimate = $('.item-clock-' + id);
                    if (obj.data('_time') == undefined) {
                        obj.data('_time', {
                            '$seconds': 0,
                            '$id': id,
                            '$date_default': strtotime($(element).data('TargetDate')),
                            "$last_date": strtotime($(element).data('TargetDate')),
                            "$animate": false
                        });
                    }
                    objAnimate.find('.iclock-painel').remove();
                    var $elData = obj.data('_time');
                    $elData.$seconds = secs;

                    if ($elData.$date_default != $elData.$last_date) {
                        $(element).data('TargetDate', date('Y-m-d H:i:s', $elData.$last_date));
                        $elData.$animate = true;

                    }
                    obj.removeClass('box-green').removeClass('box-red');


                    if ($elData.$seconds < ($.onTickerleilao.$seconds / 2)) {

                        obj.addClass('box-red');
                    }
                    if ($elData.$seconds <= $.onTickerleilao.$seconds && $elData.$seconds > ($.onTickerleilao.$seconds / 2)) {
                        obj.addClass('box-green');
                    }

                    if ($elData.$seconds < clockSecondsShow) {

                        var has_clock = objAnimate.find('.iclock-painel').size();
                        if ($elData.$animate == false) {
                            animateObj(obj);
                            $elData.$animate = true;
                        }
                        if (has_clock == 0) {
                            objAnimate.append('<span class="iclock-painel"></span>')
                        }
                    }


                    obj.data('_time', $elData);

                    /*
                     var id = $(element).attr('data-lote-id'),
                     tickers = $.onTickerleilao.tickers, dafault_date = $(element).data('TargetDate'),
                     tkstamp = $.onTickerleilao.tickers_stamp,
                     total_seconds = minutes * 60;
                     
                     if ((tickers.length > 0 && tickers[id] !== dafault_date && tickers[id] !== false)) {
                     $(element).data('TargetDate', tickers[id]);
                     }
                     onTicker['data-' + id] = secs;
                     $('#painel-online-' + id).removeClass('red').removeClass('orange');
                     $('#painel-online-' + id + ' > a[role="addLance"]').removeClass('btn-danger');
                     if (secs > (total_seconds / 2) && secs <= total_seconds && secs % 2 == 0 && secs > 1) {
                     $('#painel-online-' + id).addClass('orange');
                     }
                     
                     if (secs <= (total_seconds / 2) && secs % 2 == 0 && secs > 1) {
                     $('#painel-online-' + id).addClass('red');
                     $('#painel-online-' + id + ' a[role="addLance"]').removeClass('btn-primary').addClass('btn-danger');
                     }
                     */


                },
                finish: function (element) {
                    var id = $(element).attr('data-lote-id');
                    // $('.painel-online-bt[data-lt-id="' + id + '"]').remove();
                    var id = $(element).attr('data-lote-id'),
                            obj = $('[data-lote-id="' + id + '"]'),
                            objAnimate = $('.item-clock-' + id);
                    objAnimate.find('.iclock-painel').remove();
                    onExpiresCount(element);
                }
            });
    // atualizeLancesPainelAtivos(url_site + 'home/getDataAtivosLances');
    atualizeLancesPainel(url_site + 'ping-lances.html');


    function onExpiresCount(element) {
        var id = $(element).attr('data-lote-id'),
                markup = '<div class="box-encerrado"></div>' +
                '<div class="text-encerrado"><span class="glyphicon glyphicon-ban-circle"></span> Encerrado</div>';
        /*
         $('#painel-online-' + id).prepend(markup).delay(15000).fadeOut('1000', function() {
         $(element).remove();
         });*/
        var id = $(element).attr('data-id'),
                obj = $('[data-lote-id="' + id + '"]'),
                lote = $(element).attr('data-lote-id'),
                leilao = $(element).attr('data-leilao-id'),
                markup = '<div class="box-encerrado"></div>' +
                '<div class="text-encerrado"><span class="glyphicon glyphicon-ban-circle"></span> Encerrado</div>';
        var idata = {
            lote: lote,
            leilao: leilao
        };

        $.ajax({
            url: url_site + 'home/onCloseLote',
            dataType: 'json',
            type: 'post',
            data: idata,
            success: function (data) {
                location.reload();

            }
        });
    }




    $('.info-painel-online-incremento').tooltip();
    // $('#clock-painel-online').clock({"format": "24", "calendar": "false"});

    //panel-history
    function updateHistoryPainel() {
        $('.on-panel-history').each(function () {
            var id = $(this).attr('data-id'), obj = this;
            $.ajax({
                url: '/home/getHistoryAtivo/' + id,
                success: function (html) {
                    $(obj).attr('data-content', '<div class="panel-history">' + html + '</div>');
                    $('.table-history tbody').find('tr').eq(0).addClass('success');
                }
            });

        }).popover({
            html: true
        });

        setTimeout(updateHistoryPainel, 15000);

    }
    updateHistoryPainel();




});



